package com.customers.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.customers.entity.Customer;
import com.customers.service.CustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/contactForm")
public class ContactFormServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        Integer id = Integer.parseInt(request.getParameter("id"));

        List<Customer> customers  = null;
        try {
            CustomerService customerService = (CustomerService) request.getServletContext().getAttribute("CustomerService");
            String sql = "contactById";
            customers = customerService.searchForContact(id,sql);
//            customers = ContactFormService.searchForContact(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //查询结束，返回结果
            String formjson = JSON.toJSONString(customers, JSONWriter.Feature.WriteNullStringAsEmpty);
            response.getWriter().print(formjson);
    }
}
